<?php 
/**
* 货品列表控制器
*/
class GoodsListController extends Controller{
	public $model;
	public function __init(){
		parent::__init();
		$this->model = K('GoodsList');
	}

	public function index(){
		$gid = Q('get.gid',0,'intval');
		$type_tid = Q('get.tid',0,'intval');
		// p($gid);
		// p($type_tid);
		// 获得商品信息
		$data = $this->model->where("goods_gid={$gid}")->all();
		$this->assign('data',$data);		

		// 获得该商品对应的所有货品
		$goosList = $this->model->where("goods_gid={$gid} AND is_show=1")->all();
		// 循环所有货品
		foreach ($goosList as $k => $v) {
			// 将货品的组合id转为数组
			$combines = explode(',', $v['combine']);
			// 循环组合id数组
			$attr=array();
			foreach ($combines as $key => $value) {
				// 将组合id作为键名，商品属性表中id为这个的信息作为键值重新组合成一个数组
				$attr[]=K('GoodsAttr')->where("gaid=$value")->find();
			}
			// 将新组合的数组压入到所有货品数组中
			$goosList[$k]['combines']=$attr;
		}
		// p($goosList);
		$this->assign('goosList',$goosList);

		// 获得商品的类型规格
		$typeData = K('Type_attr')->where("type_tid={$type_tid} AND class=1")->all();
		foreach ($typeData as $k => $v) {
			$typeData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		// p($typeData);
		$this->assign('typeData',$typeData);
		
		// 将上面获得的类型id重新组合
		$taid = array();
		foreach ($typeData as $v) {
			$taid[] = $v['taid'];
		}
		$attr_good = array();
		foreach ($taid as $v) {
			$attr_good[$v]=K('GoodsAttr')->where("type_attr_taid=$v AND goods_gid={$gid}")->all();
		}
		// p($attr_good);
		$this->assign('attr_good',$attr_good);

		// 添加货品
		if(IS_POST){
			// p(Q('post.'));die;
			$data = array(
				'combine'=>implode(',', Q('post.spec')),
				'inventory'=>Q('post.inventory'),
				'number'=>Q('post.number'),
				'goods_gid'=>Q('post.goods_gid')
			);
			$this->model->add($data);
			$this->success('添加成功',U('index',array('gid'=>$gid,'tid'=>$type_tid)));
		}

		$this->display();
	}

	// 编辑货品
	public function edit(){
		$glid = Q('get.glid',0,'intval');
		// 获得旧数据
		$oldData = $this->model->where("glid={$glid}")->find();
		// $oldData['combine'] = explode(',', $oldData['combine']);
		// p($oldData);die;
		$this->assign('oldData',$oldData);

		// 通过旧数据中的商品id在商品表里找到类型id
		$good = K('Goods')->where("gid={$oldData['goods_gid']}")->find();
		// p($good);
		$tid = $good['type_tid'];
		// 获得商品的类型规格
		$typeData = K('Type_attr')->where("type_tid={$tid} AND class=1")->all();
		foreach ($typeData as $k => $v) {
			$typeData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		// p($typeData);
		$this->assign('typeData',$typeData);

		// 将上面获得的类型id重新组合
		$taid = array();
		foreach ($typeData as $v) {
			$taid[] = $v['taid'];
		}
		$attr_good = array();
		foreach ($taid as $k => $v) {
			$attr_good[$v]=K('GoodsAttr')->where("type_attr_taid=$v AND goods_gid={$good['gid']}")->all();
		}
		// p($attr_good);
		$this->assign('attr_good',$attr_good);


		// 获得商品属性
		$combine = explode(',', $oldData['combine']);
		$attr = array();
		foreach ($combine as $k => $v) {
			$attr[] = K('GoodsAttr')->where("gaid={$v}")->find();
		}
		// p($attr);
		$this->assign('attr',$attr);

		if(IS_POST){
			// p(Q('post.'));die;
			$data = array(
				'combine'=>implode(',', Q('post.spec')),
				'inventory'=>Q('post.inventory'),
				'number'=>Q('post.number'),
				'goods_gid'=>$oldData['goods_gid']
			);
			$this->model->where("glid={$glid}")->update($data);
			$this->success('修改成功',U('index',array('gid'=>$oldData['goods_gid'],'tid'=>$tid)));
		}
		$this->display();
	}

	// 删除货品
	public function del(){
		$glid = Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		$this->success('删除成功',U('index'));
	}


}
 ?>